﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using ArgumentNullException= ZO.SmartCore.Core.ArgumentNullException;

#endregion

namespace ZO.SmartCore.My4CSharp
{
    /// <summary>Provides properties for getting the information about the application, such as the version number, description, loaded assemblies, and so on.</summary>
    [HostProtection(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)]
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    public class AssemblyInfo
    {
        #region Constructors

        /// <summary>Initializes a new instance of the <see cref="AssemblyInfo"></see> class with the specified assembly information. </summary>
        /// <param name="currentAssembly"><see cref="T:System.Reflection.Assembly"></see>. The assembly for which to obtain the information.</param>
        public AssemblyInfo(Assembly currentAssembly)
        {
            this._Description = null;
            this._Title = null;
            this._ProductName = null;
            this._CompanyName = null;
            this._Trademark = null;
            this._Copyright = null;
            if (currentAssembly == null)
            {
                throw new ArgumentNullException("currentAssembly");
            }
            this._Assembly = currentAssembly;
        }

        #endregion

        #region Methods

        private object GetAttribute(Type AttributeType)
        {
            object[] objArray1 = this._Assembly.GetCustomAttributes(AttributeType, true);
            if (objArray1.Length == 0)
            {
                return null;
            }
            return objArray1[0];
        }
        #endregion



        #region Properties

        /// <summary>Gets the name, without the extension, of the assembly file for the application.</summary>
        /// <returns>A String containing the file name.</returns>
        public string AssemblyName
        {
            get
            {
                return this._Assembly.GetName().Name;
            }
        }


        /// <summary>Gets the company name associated with the application.</summary>
        /// <returns>A String that contains the company name associated with the application.</returns>
        public string CompanyName
        {
            get
            {
                if (this._CompanyName == null)
                {
                    AssemblyCompanyAttribute attribute1 = (AssemblyCompanyAttribute)this.GetAttribute(typeof(AssemblyCompanyAttribute));
                    if (attribute1 == null)
                    {
                        this._CompanyName = "";
                    }
                    else
                    {
                        this._CompanyName = attribute1.Company;
                    }
                }
                return this._CompanyName;
            }
        }

        /// <summary>Gets the copyright notice associated with the application.</summary>
        /// <returns>A String containing the copyright notice associated with the application.</returns>
        public string Copyright
        {
            get
            {
                if (this._Copyright == null)
                {
                    AssemblyCopyrightAttribute attribute1 = (AssemblyCopyrightAttribute)this.GetAttribute(typeof(AssemblyCopyrightAttribute));
                    if (attribute1 == null)
                    {
                        this._Copyright = "";
                    }
                    else
                    {
                        this._Copyright = attribute1.Copyright;
                    }
                }
                return this._Copyright;
            }
        }

        /// <summary>Gets the description associated with the application.</summary>
        /// <returns>A String containing the description associated with the application.</returns>
        public string Description
        {
            get
            {
                if (this._Description == null)
                {
                    AssemblyDescriptionAttribute attribute1 = (AssemblyDescriptionAttribute)this.GetAttribute(typeof(AssemblyDescriptionAttribute));
                    if (attribute1 == null)
                    {
                        this._Description = "";
                    }
                    else
                    {
                        this._Description = attribute1.Description;
                    }
                }
                return this._Description;
            }
        }

        /// <summary>Gets the directory where the application is stored.</summary>
        /// <returns>A String that contains the directory where the application is stored.</returns>
        public string DirectoryPath
        {
            get
            {
                return Path.GetDirectoryName(this._Assembly.Location);
            }
        }

        /// <summary>Gets a collection of all assemblies loaded by the application.</summary>
        /// <returns>A <see cref="T:System.Collections.ObjectModel.ReadOnlyCollection`1"></see> of <see cref="T:System.Reflection.Assembly"></see> containing all the assemblies loaded by the application.</returns>
        public ReadOnlyCollection<Assembly> LoadedAssemblies
        {
            get
            {
                Collection<Assembly> collection2 = new Collection<Assembly>();
                foreach (Assembly assembly1 in AppDomain.CurrentDomain.GetAssemblies())
                {
                    collection2.Add(assembly1);
                }
                return new ReadOnlyCollection<Assembly>(collection2);
            }
        }

        /// <summary>Gets the product name associated with the application.</summary>
        /// <returns>A String containing the product name associated with the application.</returns>
        public string ProductName
        {
            get
            {
                if (this._ProductName == null)
                {
                    AssemblyProductAttribute attribute1 = (AssemblyProductAttribute)this.GetAttribute(typeof(AssemblyProductAttribute));
                    if (attribute1 == null)
                    {
                        this._ProductName = "";
                    }
                    else
                    {
                        this._ProductName = attribute1.Product;
                    }
                }
                return this._ProductName;
            }
        }

        /// <summary>Gets the current stack-trace information.</summary>
        /// <returns>A String containing the current stack-trace information. The return value can be <see cref="F:System.String.Empty"></see>.</returns>
        public string StackTrace
        {
            get
            {
                return Environment.StackTrace;
            }
        }
        /// <summary>Gets the title associated with the application.</summary>
        /// <returns>A String containing the <see cref="T:System.Reflection.AssemblyTitleAttribute"></see> associated with the application.</returns>
        public string Title
        {
            get
            {
                if (this._Title == null)
                {
                    AssemblyTitleAttribute attribute1 = (AssemblyTitleAttribute)this.GetAttribute(typeof(AssemblyTitleAttribute));
                    if (attribute1 == null)
                    {
                        this._Title = "";
                    }
                    else
                    {
                        this._Title = attribute1.Title;
                    }
                }
                return this._Title;
            }
        }

        /// <summary>Gets the trademark notice associated with the application.</summary>
        /// <returns>A String containing the trademark notice associated with the application.</returns>
        public string Trademark
        {
            get
            {
                if (this._Trademark == null)
                {
                    AssemblyTrademarkAttribute attribute1 = (AssemblyTrademarkAttribute)this.GetAttribute(typeof(AssemblyTrademarkAttribute));
                    if (attribute1 == null)
                    {
                        this._Trademark = "";
                    }
                    else
                    {
                        this._Trademark = attribute1.Trademark;
                    }
                }
                return this._Trademark;
            }
        }

        /// <summary>Gets the version number of the application.</summary>
        /// <returns>A <see cref="T:System.Version"></see> object containing the version number of the application.</returns>
        public Version Version
        {
            get
            {
                return this._Assembly.GetName().Version;
            }
        }

        /// <summary>Gets the amount of physical memory mapped to the process context.</summary>
        /// <returns>A Long containing the number of bytes of physical memory mapped to the process context.</returns>
        public long WorkingSet
        {
            get
            {
                return Environment.WorkingSet;
            }
        }
        #endregion
        #region Fields

        private Assembly _Assembly;
        private string _CompanyName;
        private string _Copyright;
        private string _Description;
        private string _ProductName;
        private string _Title;
        private string _Trademark;
        #endregion
    }
}
